<!-- 功能：创建SNMPv3用户 弹窗 | 作者：menghao.yan | 邮箱：yxs2615@163.com | 时间：2023年08月21日 22:59:06 -->
<template>
  <!-- 弹出框 -->
  <a-modal v-model:open="visible" title="创建SNMPv3用户" width="500px" class="y-modal" okText="创建" @ok="onSubmit" @cancel="onCancel">
    <a-form :label-col="{ span: 6 }" :wrapper-col="{span: 14}">
      <a-form-item label="用户名" v-bind="validateInfos.name">
        <a-input v-model:value="modelRef.name" placeholder="请输入用户名" />
      </a-form-item>
      <a-form-item label="权限" v-bind="validateInfos.quanXian">
        <a-select v-model:value="modelRef.quanXian" placeholder="请选择权限">
          <a-select-option value="read">只读用户</a-select-option>
          <a-select-option value="write">可读/写的用户</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="密钥" v-bind="validateInfos.miYao">
        <a-input v-model:value="modelRef.miYao" placeholder="请输入密钥" />
      </a-form-item>
      <a-form-item label="密钥加密方式" v-bind="validateInfos.miYaoJiaMiFangShi">
        <a-select v-model:value="modelRef.miYaoJiaMiFangShi" placeholder="请选择密钥加密方式">
          <a-select-option value="md5">MD5</a-select-option>
          <a-select-option value="sha">SHA</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="私钥" v-bind="validateInfos.siYao">
        <a-input v-model:value="modelRef.siYao" placeholder="请输入私钥" />
      </a-form-item>
      <a-form-item label="私钥加密方式" v-bind="validateInfos.siYaoJiaMiFangShi">
        <a-select v-model:value="modelRef.siYaoJiaMiFangShi" placeholder="请选择密钥加密方式">
          <a-select-option value="des">DES</a-select-option>
          <a-select-option value="aes">AES</a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const { visible } = toRefs(props)

// 定义组件的事件
const emits = defineEmits(['update:visible'])

import { Form } from 'ant-design-vue'

const useForm = Form.useForm

const modelRef = reactive({
  name: '',
  quanXian: 'read',
  miYao: '',
  miYaoJiaMiFangShi: 'md5',
  siYao: '',
  siYaoJiaMiFangShi: 'des'
})

const rulesRef = reactive({
  name: [
    {
      required: true,
      message: '请输入用户名'
    }
  ],
  quanXian: [
    {
      required: true,
      message: '请选择权限'
    }
  ],
  miYao: [
    {
      required: true,
      message: '请输入密钥'
    }
  ],
  miYaoJiaMiFangShi: [
    {
      required: true,
      message: '请选择密钥加密方式'
    }
  ],
  siYao: [
    {
      required: true,
      message: '请输入私钥'
    }
  ],
  siYaoJiaMiFangShi: [
    {
      required: true,
      message: '请选择私钥加密方式'
    }
  ]
})

const { resetFields, validate, validateInfos, mergeValidateInfo } = useForm(modelRef, rulesRef)
const onSubmit = () => {
  validate()
    .then(() => {
      console.log('创建SNMPv3用户>>', toRaw(modelRef))
      onCancel()
    })
    .catch(err => {
      console.log('error', err)
    })
}

const onCancel = () => {
  // 触发自定义事件来通知父组件关闭弹窗
  emits('update:visible', false)
}
</script>

<style scoped>
.ant-row {
  margin-bottom: 10px;
}
.y-xinJianYongHu-title {
  color: #333;
  font-weight: bold;
  font-size: 16px;
}
</style>